<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Foundation\Auth\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Templates;
use App\Models\AdminUser;
use App\Models\Group;
use App\Models\Domain;
use App\Models\Wechat;
use App\Models\CopyWechat;
use App\Models\Originate;
use App\Models\CopyAccount;

use Log,DB,Cache, Event,Schema;
use App\Events\permChangeEvent;

class IndexController extends Controller
{
    public function index(Request $request)
    {
    	ini_set('max_execution_time', '0');
// 		$arrAcc = [
// '2019-08-11'=>'321.01',
// '2019-08-10'=>'413.01',
// '2019-08-09'=>'3102.51',
// '2019-08-08'=>'3148.2',
// '2019-08-07'=>'3054.16',
// '2019-08-06'=>'2423.05',
// '2019-08-05'=>'2979.6',
// '2019-08-04'=>'2124.36',
// '2019-08-03'=>'496.57',
// '2019-08-02'=>'1630.33',
// '2019-08-01'=>'1243.93',
// '2019-07-31'=>'947.38',
// '2019-07-30'=>'1320.46',
// '2019-07-29'=>'1534.54',
// '2019-07-28'=>'2190.4',
// '2019-07-27'=>'2111.7',
// '2019-07-26'=>'1154',
// '2019-07-20'=>'0',
// '2019-07-19'=>'1080.35',
// '2019-07-18'=>'3051.72',
// '2019-07-17'=>'1971.15',
// '2019-07-16'=>'1023.11',
// '2019-07-15'=>'4144.81',
// '2019-07-14'=>'4927.67',
// '2019-07-13'=>'10461.13',
// '2019-07-12'=>'13849.96',
// '2019-07-11'=>'6763.17',
// '2019-07-10'=>'11894.51',
// '2019-07-09'=>'14663.02',
// '2019-07-08'=>'14623.48',
// '2019-07-07'=>'10575.9',
// '2019-07-06'=>'10284.72',
// '2019-07-05'=>'14607.28',
// '2019-07-04'=>'12670.88',
// '2019-07-03'=>'14780.41',
// '2019-07-02'=>'15718.66',
// '2019-07-01'=>'16818.97',




// 		];
// 		$acc = 'ump.yyrrlb.top';
// foreach ($arrAcc as $key => $value) {
// 	$acMap['domain'] = $acc;
// 	$acMap['gaccount'] = $acc;
// 	$acMap['dated'] = $key;
// 	CopyAccount::where('consume',0)->where($acMap)->update(['consume'=>$value]);
// }
// 	 $listsResAvgCostCP = CopyAccount::where('consume','<>',0)->orWhere('client','<>',0)->orderBy('dated','desc')->get();
// 	foreach ($listsResAvgCostCP as $key => $obj) {
// 		if( $obj->sum_copy ) $obj->cp_cost= round($obj->consume / $obj->sum_copy);
// 		if( $obj->client ) $obj->rmb_cost= round($obj->consume / $obj->client);
// 		$obj->save();
// 	}
// echo 'yesFinish：'.$acc;exit;
    	$tongjiTime = strtotime('-1 day');
    	$todayDate = date('Y-m-d',$tongjiTime);
    	$todayDateStartTime = date('Y-m-d 00:00:00',$tongjiTime);
    	$todayDateEndTime = date('Y-m-d 23:59:59',$tongjiTime);
    	// echo $todayDateStartTime;echo '<br>';echo $todayDateEndTime;

    	$SumCopy = CopyWechat::select('id','ip')->where('dated',$todayDate)->get()->unique('ip')->count();
    	$SumCACopy = CopyAccount::select('sum_copy')->where('dated',$todayDate)->sum('sum_copy');
    	// dd($SumCopy);
    	if ($SumCopy != $SumCACopy) {
	    	$listsCopyDomain = CopyWechat::select('uid','domain','channel')->whereBetween('created_at',[$todayDateStartTime,$todayDateEndTime])->groupBy('domain')->get();
	    	$listsCopyDomainCA = CopyAccount::select('uid','domain')->where('dated',$todayDate)->groupBy('domain')->get();
	    	// dd($listsCopyDomainCA->count());exit;
	    	foreach ($listsCopyDomainCA as $keyCA => $objCA) {
	    		foreach ($listsCopyDomain as $key => $obj) {
	    			if ($obj->domain == $objCA->domain && $obj->uid == $objCA->uid) {
	    				unset($listsCopyDomain[$key]);
	    			}
	    		}
	    	}
	    	// dd($listsCopyDomain->count());exit;
	    	if( $listsCopyDomain->count() ){
		    	foreach ($listsCopyDomain as $key => $obj) {
		    		$obj->gaccount = $obj->domain;
		    		$obj->dated = $todayDate;
		    		$obj->created_at        = date('Y-m-d H:i:s');
		    		$obj->updated_at        = date('Y-m-d H:i:s');
		    		$listsCopyDomain[$key] = $obj;	
		    	}
		    	$res = CopyAccount::insert($listsCopyDomain->toArray());
	    	}
    		$listsCopyDomainCA = CopyAccount::select('id','sum_copy','uid','domain','dated')->where('dated',$todayDate)->orderBy('dated','desc')->get();
    		foreach ($listsCopyDomainCA as $key => $obj) {
    		$map['uid'] = $obj->uid;
    		$map['domain'] = $obj->domain;
    		$map['dated'] = $obj->dated;
    		$sum_copy = CopyWechat::select('id','ip')->where($map)->get()->unique('ip')->count();
    		$obj->sum_copy = $sum_copy;
    		$obj->save();
    		}
    		
    	}
    	
    	// exit;

    	// --更新时间
		// $list = CopyWechat::select('id','created_at','timed')->where('timed',-1)->skip(0)->take(10000)->groupBy('id')->get();
		// foreach ($list as $key => $obj) {
		// 	$obj->yeard = date('Y',strtotime($obj->created_at));
		// 	$obj->dated = date('Y-m-d',strtotime($obj->created_at));
		// 	$obj->timed = date('H',strtotime($obj->created_at));
		// 	$obj->save();
		// }
		// --更新时间
    	// exit;
    	
    	// DB::connection()->disableQueryLog(); 
    	// 21:00 ~21:00
		$selectTime = time();
		$needTimeHour = ' 00:00';
		$startTime = $yesterdayTime = date('Y-m-d',strtotime("-1 day")) .$needTimeHour;
		// $endTime = $todayTime = date('Y-m-d',$selectTime).$needTimeHour;
		// $tomorrowTime = date('Y-m-d',strtotime("+1 day")).$needTimeHour;
		$startTime = $todayTime = date('Y-m-d');
		$endTime = $tomorrowTime = date('Y-m-d',strtotime("+1 day"));
		$data['time']['yesterdayTime'] = $yesterdayTime ;
		$data['time']['todayTime'] = $todayTime ;
		$data['time']['tomorrowTime'] = $tomorrowTime ;
		
		$hour = date('H') * 1;
		if($hour > 20){
			$startTime = $todayTime;
			$endTime = $tomorrowTime;
		}
		// $show = DB::connection('mysql2')->table('groups')->orderBy('id','desc')->get();
		// foreach ($show as $key => $obj) {
		// 	echo $obj->username .'--'.$obj->title .'--'.'<br>';
		// }
		// exit;
		$optime = isset($_GET['optime']) ? $_GET['optime'] : 'all';
		switch ($optime) {
			case 'today':
				$startTime = $todayTime = date('Y-m-d');
				$endTime = $tomorrowTime = date('Y-m-d',strtotime("+1 day"));
				break;
			case 'yesterday':
				$startTime = $todayTime = date('Y-m-d',strtotime("-1 day"));
				$endTime = $tomorrowTime = date('Y-m-d');
				break;
			case 'lastTwoDay':
				$startTime = $todayTime = date('Y-m-d',strtotime("-2 day"));
				$endTime = $tomorrowTime = date('Y-m-d',strtotime("-1 day"));
				break;
			case 'lastThreeDay':
				$startTime = $todayTime = date('Y-m-d',strtotime("-3 day"));
				$endTime = $tomorrowTime = date('Y-m-d',strtotime("-2 day"));
				break;
			case 'lastTwoDayAll':
				$startTime = $todayTime = date('Y-m-d',strtotime("-2 day"));
				$endTime = $tomorrowTime = date('Y-m-d');
				break;
			case 'lastSevenDay':
				$startTime = $todayTime = date('Y-m-d',strtotime("-6 day"));
				$endTime = $tomorrowTime = date('Y-m-d');
				break;
			case 'lastMonthDay':
				$startTime = $todayTime = date('Y-m-d',strtotime("-30 day"));
				$endTime = $tomorrowTime = date('Y-m-d');
				break;
			case 'history':
				$startTime = $todayTime = '2017-1-1';
				$endTime = $tomorrowTime = date('Y-m-d',strtotime("+1 day"));
				break;
			
			default:
				# code...
				break;
		}
		

		// echo $startTime .'<br>' . $endTime;

		//CacheTime minutes
		$cacheMinutes = 600;

		if($request->cache=='_ou7i44y7j22d88yr83'){
			Event::fire(new permChangeEvent());
			return redirect('/admin/index')->withSuccess('Success');
		}
		

		//最近七天的复制的域名列表
	 	$data['listsDomain']= CopyWechat::select('domain','channel')->whereBetween('created_at',[date('Y-m-d',strtotime("-6 day")),date('Y-m-d',strtotime("+1 day"))])->groupBy('domain')->orderBy('channel','desc')->get();

	 	
		// dd($listsDomain);
		if(isset($_GET['op'])){
			$op = $_GET['op'];
			$data['domain']=$request->get('domain');
			switch ($op) {
				case 'checkdomain':
					$data['domain']=$_GET['domain'];
					//复制数据
					$listsCopy = CopyWechat::where('domain',$data['domain'])->whereBetween('created_at',[$startTime,$endTime])->orderBy('id','desc')->get();
					$data['listsCopy'] = $listsCopy->unique('ip');
					// ideaIdPosition
					$data['KeywordListAd'] = $listsCopy->unique('ip')->groupBy('keyword');
					foreach ($data['KeywordListAd'] as $key => $value) {
						$data['KeywordListAd2'][$key]=count($value);
					}
					$data['KeywordListAd'] = $data['KeywordListAd2'];
					arsort($data['KeywordListAd']);

					
					// Tpl
					$data['TemplateListAd'] = $listsCopy->unique('ip')->groupBy('template');
					foreach ($data['TemplateListAd'] as $key => $value) {
						$data['TemplateListAd2'][$key]=count($value);
					}
					$data['TemplateListAd'] = $data['TemplateListAd2'];
					arsort($data['TemplateListAd']);
					return view('admin.index.dataTable',$data);
					break;

				case 'checkOnline':
					//实时在线统计
					if($request->get('domain') == 'all'){
						// 最近一分钟数据
						// $startTime = date('Y-m-d H:i:0',strtotime("-2 minutes"));
						// $endTime = date('Y-m-d H:i:0',strtotime("-1 minutes"));
						$startTime = date('Y-m-d H:i:s',strtotime("-1 minutes"));
						$endTime = date('Y-m-d H:i:s');
						// echo $startTime .' ---- ' .$endTime;

						$data['online']['listsOriginate'] = $listsOriginate = Originate::whereBetween('created_at',[$startTime,$endTime])->orderBy('id','desc')->get();

						$data['online']['onlyIp'] = $listsOriginate->unique('ip')->count();
						$data['online']['onlyPv'] = $listsOriginate->count();
						$data['online']['channeList'] = $listsOriginate->unique('ip')->groupBy('channel');
						$data['online']['domainList'] = $listsOriginate->unique('ip')->groupBy('domain');
						foreach ($data['online']['domainList'] as $key => $value) {
							$data['online']['domainList2'][$key]=count($value);
						}
						arsort($data['online']['domainList2']);
						// dd($data['online']['domainList2']);
						// 最近15分钟数据
						// $startTime = date('Y-m-d H:i:0',strtotime("-16 minutes"));
						// $endTime = date('Y-m-d H:i:0',strtotime("-1 minutes"));
						$startTime = date('Y-m-d H:i:s',strtotime("-15 minutes"));
						$endTime = date('Y-m-d H:i:s');
						$data['online']['listsOriginateLast15M'] = array();
						$data['online']['listsOriginateLast15M'] = $listsOriginateLast15M = Originate::whereBetween('created_at',[$startTime,$endTime])->orderBy('id','desc')->get();
						$data['online']['onlyIpLast15M'] = $listsOriginateLast15M->unique('ip')->count();
						$data['online']['onlyPvLast15M'] = $listsOriginateLast15M->count();

					}else{
						// 最近一分钟数据
						// $startTime = date('Y-m-d H:i:0',strtotime("-2 minutes"));
						// $endTime = date('Y-m-d H:i:0',strtotime("-1 minutes"));
						$startTime = date('Y-m-d H:i:s',strtotime("-1 minutes"));
						$endTime = date('Y-m-d H:i:s');
						// echo $startTime .' ---- ' .$endTime;

						$data['online']['listsOriginate'] = $listsOriginate = Originate::where('domain',$data['domain'])->
									 		 				whereBetween('created_at',[$startTime,$endTime])->orderBy('id','desc')->get()->unique('touch');
						// dd($data['online']['listsOriginate']);
						$data['online']['onlyIp'] = $listsOriginate->unique('ip')->count();
						$data['online']['onlyPv'] = $listsOriginate->count();
						$data['online']['channeList'] = $listsOriginate->unique('ip')->groupBy('channel');
						$data['online']['domainList'] = $listsOriginate->unique('ip')->groupBy('domain');
						$data['online']['domainList2'] = array();
						foreach ($data['online']['domainList'] as $key => $value) {
							$data['online']['domainList2'][$key]=count($value);
						}
						if(!empty($data['online']['domainList2'])){
							arsort($data['online']['domainList2']);
						}
						
						// dd($data['online']['domainList']);
						// 最近15分钟数据
						// $startTime = date('Y-m-d H:i:0',strtotime("-16 minutes"));
						// $endTime = date('Y-m-d H:i:0',strtotime("-1 minutes"));
						$startTime = date('Y-m-d H:i:s',strtotime("-3600 minutes"));
						$endTime = date('Y-m-d H:i:s');
						$data['online']['listsOriginateLast15M'] = array();
						$data['online']['listsOriginateLast15M'] = $listsOriginateLast15M = Originate::where('domain',$data['domain'])->
																	whereBetween('created_at',[$startTime,$endTime])->orderBy('id','desc')->get()->unique('touch');
						$data['online']['onlyIpLast15M'] = $listsOriginateLast15M->unique('ip')->count();
						$data['online']['onlyPvLast15M'] = $listsOriginateLast15M->count();
					}


					// var_dump($data);
					//来源数据
					// $listsOR = Originate::where('domain',$data['domain'])->whereBetween('created_at',[$startTime,$endTime])->orderBy('id','desc')
					// ->skip(0)->take(500)->get();
					// $data['listsOR'] = $listsOR->unique('ip');
					return view('admin.index.online',$data);
					break;

				case 'checKF':

					if($request->get('domain') == 'all'){
						if(  !is_null( $request->get('kfwx') )){
							$listsCopy = CopyWechat::where('kfwx',$request->get('kfwx'))->whereBetween('created_at',[$startTime,$endTime])->
									 			 orderBy('id','desc')->get();
							$data['listsCopy'] = $listsCopy->unique('ip');
						}else{
							$dlists = CopyWechat::select('kfwx','ip')->whereBetween('created_at',[$startTime,$endTime])->get();
							$data['listsCopy'] = $dlists->unique('ip')->groupBy('kfwx');
							$data['listsCopy2'] = array();
							foreach ($data['listsCopy'] as $key => $value) {
								$data['listsCopy2'][$key]=count($value);
							}
							
							if(!empty($data['listsCopy2'])){
								arsort($data['listsCopy2']);
							}
							// dd($data['listsCopy']);
							// $data['listsCopy'] = CopyWechat::select(DB::raw('count(*) as count,kfwx'))->
							// 		 whereBetween('created_at',[$startTime,$endTime])->groupBy('kfwx')->orderBy('count','desc')->get();
							// dd($data['listsCopy2']);
						}
						
					}else{
						$data['domain']=$request->get('domain');
						//复制数据
						$data['listsCopy'] = CopyWechat::select(DB::raw('count(*) as count,kfwx'))->
									 		 where('domain',$data['domain'])->whereBetween('created_at',[$startTime,$endTime])->groupBy('kfwx')->orderBy('count','desc')->get();
					}

				
					//来源数据
					// $listsOR = Originate::where('domain',$data['domain'])->whereBetween('created_at',[$startTime,$endTime])->orderBy('id','desc')
					// ->skip(0)->take(500)->get();
					// $data['listsOR'] = $listsOR->unique('ip');
					return view('admin.index.kefu',$data);
					break;
				case 'checOriginate':
					//来源数据
					
					// if(isset($_GET['keyword']) && $_GET[''])
					if($request->get('or_type') == 'keyword'){
						//关键词
						if(isset($_GET['keyword'])){
							$listsOR = Originate::select('keyword','touch','domain','channel')->where('keyword',$_GET['keyword'])->whereNotIn('keyword',['','无法识别','无'])->whereBetween('created_at',[$startTime,$endTime])->groupBy('keyword','touch')
						    ->skip(0)->take(200)->get();
						   
						}else{
							$listsOR = Originate::select(DB::raw('count(*) as count,keyword,domain,channel'))->whereNotIn('keyword',['','无法识别','无'])->whereBetween('created_at',[$startTime,$endTime])->groupBy('keyword')->orderBy('count','desc')
						    ->skip(0)->take(50)->get();
						     // dd($listsOR);
						}
						

					}else{
						//搜索词
						
					    if(isset($_GET['keyword'])){
					    	$listsOR = Originate::select(DB::raw('count(*) as count,touch,domain,channel','channel'))->where('keyword',$_GET['keyword'])->whereBetween('created_at',[$startTime,$endTime])->groupBy('touch')->orderBy('count','desc')
					        ->skip(0)->take(200)->get();
					    }else{
					    	$listsOR = Originate::select(DB::raw('count(*) as count,touch,domain,channel','channel'))->whereNotIn('touch',['','无法识别搜索词'])->whereBetween('created_at',[$startTime,$endTime])->groupBy('touch')->orderBy('count','desc')
					        ->skip(0)->take(200)->get();
					    }
					}
					


					// dd($listsOR);
					$data['listsOR'] = $listsOR;
					return view('admin.index.originate',$data);
					break;
				case 'checkDelete':
					//来源数据
					$jf_keyword = array('减肥','瘦','肚腩','赘肉','减肚' );
					// dd($jf_keyword);
					$user = DB::select('select touch from s_originate2s WHERE touch NOT REGEXP "减肥|瘦|肚腩|赘肉|减肚" limit 50'); 
					// $listsOR = Originate::select(DB::raw('touch NOT REGEXP "减肥|瘦|肚腩|赘肉|减肚",touch,keyword '))->whereBetween('created_at',[$startTime,$endTime])->groupBy('keyword')->orderBy('touch','desc')
					// 	    ->skip(0)->take(50)->get();
					// dd($listsOR);
					// exit;
					// if(isset($_GET['keyword']) && $_GET[''])
					if($request->get('or_type') == 'keyword'){
						//关键词

						if(isset($_GET['keyword'])){
							$listsOR = Originate::select(DB::raw('touch NOT REGEXP "减肥|瘦|肚腩|赘肉|减肚",count(*) as count,keyword'))->where('touch',$_GET['keyword'])->whereNotIn('keyword',['','无法识别','无'])->whereBetween('created_at',[$startTime,$endTime])->groupBy('keyword')->orderBy('count','desc')
						    ->skip(0)->take(200)->get();
						}else{
							// $listsOR = Originate::select(DB::raw('touch NOT REGEXP "减肥|瘦|肚腩|赘肉|减肚",keyword,touch,domain'))->whereBetween('created_at',[$startTime,$endTime])
							// 	    ->skip(0)->take(50)->get();
							$listsOR = DB::select('select keyword,touch,domain,created_at from s_originate2s WHERE touch NOT REGEXP "减肥|瘦|肚腩|赘肉|减肚" and where created_at between '.$startTime.' and  ' .$endTime ); 	    
							dd($listsOR);
							// $listsOR = Originate::select(DB::raw('keyword NOT REGEXP "减肥|瘦|肚腩|赘肉|减肚",count(keyword) as count,keyword'))->whereNotIn('keyword',['','无法识别','无'])->whereBetween('created_at',[$startTime,$endTime])->groupBy('keyword')->orderBy('count','desc')
						 //    ->skip(0)->take(200)->get();
						}
						

					}else{
						//搜索词
						
					    if(isset($_GET['keyword'])){
					    	$listsOR = Originate::select(DB::raw('touch NOT REGEXP "减肥|瘦|肚腩|赘肉|减肚",count(*) as count,touch'))->where('touch',$_GET['keyword'])->whereBetween('created_at',[$startTime,$endTime])->groupBy('touch')->orderBy('count','desc')
					        ->skip(0)->take(200)->get();
					    }else{
					    	$listsOR = Originate::select(DB::raw('touch NOT REGEXP "减肥|瘦|肚腩|赘肉|减肚",count(*) as count,touch'))->whereNotIn('touch',['','无法识别搜索词'])->whereBetween('created_at',[$startTime,$endTime])
					        ->skip(0)->take(200)->get();
					    }
					}
					


					// dd($listsOR);
					$data['listsOR'] = $listsOR;
					return view('admin.index.checkDelete',$data);
					break;
				
				default:

					$data['domain']='AllDomain';
					//复制数据
					$listsCopy = CopyWechat::whereBetween('created_at',[$startTime,$endTime])->get();
					$data['listsCopy'] = $listsCopy->unique('ip');
					//来源数据
					// $listsOR = Originate::whereBetween('created_at',[$startTime,$endTime])
					// ->skip(0)->take(500)->get();
					// $data['listsOR'] = $listsOR->unique('ip');
					return view('admin.index.dataTable',$data);
					break;
			}

			
		}

		if(isset($_GET['checkCopyKeywrod'])&&$_GET['checkCopyKeywrod']==='yes'){
			$cpkResultCount = CopyWechat::select('keyword','ip')->where('keyword',$_GET['keyword'])->get()->unique('ip')->count();
			echo $_GET['keyword'] ."cpCount：" .$cpkResultCount.'<br>';
			$orkResultCount = Originate::select('keyword','ip')->where('keyword',$_GET['keyword'])->get()->unique('ip')->count();
			$copyNumPercent = round($cpkResultCount / $orkResultCount * 100,0); 
			echo $_GET['keyword'] ."-cpCount：" . $cpkResultCount ." --OriginateCount：" .$orkResultCount. " cpPercent：<span style='color:red;font-weight:bold;'>".$copyNumPercent."% <br> </span> ";
			
			echo $_GET['keyword'] ."OriginateCount：" .$orkResultCount.'<br>----------------------ToDay------------------------<br>';

			if(isset($_GET['LJH'])){
				$startTime = $todayTime = date('Y-m-d');
				if(isset($_GET['time'])){
					$timeS = $_GET['time'];
					$startTime = $todayTime = date('Y-m-d',strtotime("-$timeS day"));
				}
				$endTime = $tomorrowTime = date('Y-m-d',strtotime("+1 day"));
				
				if(isset($_GET['domain'])){
					$orNewData = Originate::select(DB::raw('count(*) as count,keyword,ip'))->where('domain',$_GET['domain'])->whereNotIn('keyword',['无','{keyword}',''])->groupBy('keyword')->orderBy('count','desc')->whereBetween('created_at',[$startTime,$endTime])->skip(0)->take(600)->get()->unique('ip');
				}else{
					$orNewData = Originate::select(DB::raw('count(*) as count,keyword,ip'))->whereNotIn('keyword',['无','{keyword}',''])->groupBy('keyword')->orderBy('count','desc')->whereBetween('created_at',[$startTime,$endTime])->skip(0)->take(600)->get()->unique('ip');
				}
				foreach ($orNewData as $key => $obj) {
					$orLastCpCount = CopyWechat::select('keyword','ip')->where('keyword',$obj->keyword)->whereBetween('created_at',[$startTime,$endTime])->get()->unique('ip')->count();
					$orLastCpCountTip = $orLastCpCount;
					if( $orLastCpCount == 0){
						$orLastCpCountTip = "<span style='color:red;font-weight:bold;'>".$orLastCpCount ."</span>  " ;
					}
					$copyNumPercent = round($orLastCpCount / $obj->count * 100,0); 
					if($copyNumPercent){
						echo $obj->keyword."-cpCount：" . $orLastCpCountTip ." --OriginateCount：" .$obj->count. " cpPercent：<span style='color:red;font-weight:bold;'>".$copyNumPercent."% <br> </span> ";
					}else{
						echo $obj->keyword."-cpCount：" . $orLastCpCountTip ." --OriginateCount：<span style='color:red;font-weight:bold;'>" .$obj->count.'</span>  cpPercent：'.$copyNumPercent.'% <br>';
					}
					
				}
		}
		exit;
			
		}

		// ----------Copy----Start

		//---------------------------last five minutes
		$lastFiveMinuteSql = date('Y-m-d H:i:s',$selectTime -300);
		$lastFiveHourSql = date('Y-m-d H:i:s',$selectTime -3600);
		$currentTimeSql = date('Y-m-d H:i:s',$selectTime );

		$data['today']['cp_last_five_minutes_count'] = CopyWechat::select('ip')->whereBetween('created_at',[$lastFiveMinuteSql,$currentTimeSql])->get()->unique('ip')->count();
		//last hour
		$data['today']['cp_last_hour_count'] = CopyWechat::select('ip')->whereBetween('created_at',[$lastFiveHourSql,$currentTimeSql])->get()->unique('ip')->count();
		//---------------------------last five minutes

		$currentData = CopyWechat::select('id','template','channel','ip')->whereBetween('created_at',[$startTime,$endTime])->get();
		//all today copy
		$data['today']['cp_count'] = $currentData->count();

		$data['today']['cp_only_count'] = $currentData->unique('ip')->count();


		$data['today']['cp_template_statistics'] = CopyWechat::select(DB::raw('count(*) as count,id, template'))->whereBetween('created_at',[$startTime,$endTime])->groupBy('template')->orderBy('count','desc')->get();
		$data['today']['cp_template_statistics2'] = $currentData->unique('ip')->groupBy('template')->toArray();
		foreach ($data['today']['cp_template_statistics2'] as $key => $value) {
			$data['today']['cp_template_statistics2'][$key] = count($value);
		}
		arsort($data['today']['cp_template_statistics2']);


		$data['today']['cp_channel_statistics'] = CopyWechat::select(DB::raw('count(*) as count,id, channel,ip'))->whereBetween('created_at',[$startTime,$endTime])->groupBy('channel')->orderBy('count','desc')->get();
		$data['today']['cp_channel_statistics2'] = $currentData->unique('ip')->groupBy('channel')->toArray();
		foreach ($data['today']['cp_channel_statistics2'] as $key => $value) {
			$data['today']['cp_channel_statistics2'][$key] = count($value);
		}
		arsort($data['today']['cp_channel_statistics2']);

		$data['today']['cp_domain_statistics'] = CopyWechat::select('ip','domain')->whereBetween('created_at',[$startTime,$endTime])->orderBy('domain','desc')->get()->unique('ip')->groupBy('domain');
		$data['today']['cp_domain_statistics2'] = array();
		foreach ($data['today']['cp_domain_statistics'] as $key => $value) {
			$data['today']['cp_domain_statistics2'][$key] = count($value);
		}
		if(!empty($data['today']['cp_domain_statistics2'])) arsort($data['today']['cp_domain_statistics2']);
		
		
		$data['today']['cp_ip'] = CopyWechat::select('id','ip','channel','keyword','domain','template','created_at','kfwx')->whereBetween('created_at',[$startTime,$endTime])->orderBy('id','desc')->skip(0)->take(30)->get()->unique('ip');
		
		$data['today']['cp_channel'] = CopyWechat::select('id','ip','channel','keyword','domain','template','created_at')->whereBetween('created_at',[$startTime,$endTime])->orderBy('created_at','desc')->skip(0)->take(10)->get();

		$dataResult= CopyWechat::select('id','created_at','yeard','dated','timed')->where('dated',null)->orderBy('id','desc')->skip(0)->take(10)->get();
		


		// dd($newDatas);
		// ----------Copy----End
        return view('admin.index.lastcopy',$data);
        // return redirect()->back();
    }

    public function FunctionName($value='')
    {
    			// ----------Copy----Start
    			//all today copy
    			$data['today']['cp_count'] = CopyWechat::select('id')->whereBetween('created_at',[$todayTime,$tomorrowTime])->count();
    			//only today
    			$data['today']['cp_only_count'] = CopyWechat::select('ip')->whereBetween('created_at',[$todayTime,$tomorrowTime])->get()->unique('ip')->count();
    			//last five minutes
    			$lastFiveMinuteSql = date('Y-m-d H:i:s',time()-300);
    			$lastFiveHourSql = date('Y-m-d H:i:s',time()-3600);
    			$currentTimeSql = date('Y-m-d H:i:s',time());
    			$data['today']['cp_last_five_minutes_count'] = CopyWechat::select('ip')->whereBetween('created_at',[$lastFiveMinuteSql,$currentTimeSql])->get()->unique('ip')->count();
    			//last hour
    			$data['today']['cp_last_hour_count'] = CopyWechat::select('ip')->whereBetween('created_at',[$lastFiveHourSql,$currentTimeSql])->get()->unique('ip')->count();
    			
    			//channel && template statistics
    			$data['today']['originate_channel_statistics'] = Originate::select(DB::raw('count(*) as count,id, channel'))->whereBetween('created_at',[$todayTime,$tomorrowTime])->groupBy('channel')->orderBy('count','desc')->get();
    			
    			$data['today']['cp_template_statistics'] = CopyWechat::select(DB::raw('count(*) as count,id, template'))->whereBetween('created_at',[$todayTime,$tomorrowTime])->groupBy('template')->orderBy('count','desc')->get();
    			

    			$data['today']['cp_domain_statistics'] = CopyWechat::select(DB::raw('count(*) as count,id, domain'))->whereBetween('created_at',[$todayTime,$tomorrowTime])->groupBy('domain')->orderBy('count','desc')->get();
    			
    			// ----------Copy----End
    			// ----------Originate----Start
    			//originate_count
    			$data['today']['originate_count'] = Originate::select('id')->whereBetween('created_at',[$todayTime,$tomorrowTime])->count();
    			//originate_only_count
    			$data['today']['originate_only_count'] = Originate::select('ip')->whereBetween('created_at',[$todayTime,$tomorrowTime])->get()->unique('ip')->count();
    			
    			$data['today']['originate_last_five_minutes_count'] = Originate::select('ip')->whereBetween('created_at',[$lastFiveMinuteSql,$currentTimeSql])->get()->unique('ip')->count();
    			//
    			$data['today']['originate_last_hour_count'] = Originate::select('ip')->whereBetween('created_at',[$lastFiveHourSql,$currentTimeSql])->get()->unique('ip')->count();

    			

    			// $hour = date('H') * 1;
    			// if($hour >22){
    				
    			// }
    			// return view('admin.index.lastcopy',$data);
    			// ----------Originate----End
    			$data['today']['cp_channel'] = CopyWechat::select('id','ip','channel','keyword','domain','template','created_at')->whereBetween('created_at',[$todayTime,$tomorrowTime])->orderBy('created_at','desc')->skip(0)->take(10)->get();
    			// serach originate
    			$data['today']['originate_keyword'] = Originate::select('id','ip','channel','keyword','domain','created_at')->whereBetween('created_at',[$todayTime,$tomorrowTime])->where('channel','sogou')->orWhere('channel','baidu')->orderBy('created_at','desc')->skip(0)->take(100)->get();
    			// besides search originate
    			$data['today']['originate_besides_search'] = Originate::select('id','ip','channel','keyword','domain','created_at')->whereBetween('created_at',[$todayTime,$tomorrowTime])->where('keyword','?')->orWhere('channel','')->orderBy('created_at','desc')->skip(0)->take(10)->get();

    			//--Historical keyword comparison
    			//Today Keyword
    			$data['today']['originate_today_keyword'] = Originate::select('id','ip','channel','keyword','domain','created_at')->where('channel','sogou')->orWhere('channel','baidu')->orderBy('created_at','desc')->whereBetween('created_at',[$todayTime,$tomorrowTime])->skip(0)->take(20)->get()->unique('ip');
    			$data['today']['originate_historical_keyword'] = Originate::select('id','ip','channel','keyword','domain','created_at')->where('channel','sogou')->orWhere('channel','baidu')->orderBy('created_at','desc')->skip(0)->take(20)->get()->unique('ip');

    			$data['today']['originate_today_keyword_count']=Originate::select(DB::raw('count(*) as count,keyword'))->whereIn('channel',['sogou','baidu','uc'])->whereBetween('created_at',[$todayTime,$tomorrowTime])->count();

    			$data['today']['originate_historical_keyword'] = Originate::select(DB::raw('count(*) as count,keyword'))->whereIn('channel',['sogou','baidu','uc'])->groupBy('keyword')->orderBy('count','desc')->skip(0)->take(50)->get();
    			
    			$data['today']['originate_historical_keyword_count']=Originate::select(DB::raw('count(*) as count,keyword'))->whereIn('channel',['sogou','baidu','uc'])->count();
    }


}
